﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CMTVision.Widgets.Framework
{
    public static class Util
    {
        public static string GenerateID<T>(string originId)
        {
            if (typeof(ITemplate).IsAssignableFrom(typeof(T)))
            {
                return Constants.ID_Prefix_Template + originId;
            }
            else if (typeof(ITemplateContainer).IsAssignableFrom(typeof(T)))
            {
                return Constants.ID_Prefix_TemplateContainer + originId;
            }
            else if (typeof(ITemplateColumn).IsAssignableFrom(typeof(T)))
            {
                return Constants.ID_Prefix_TemplateColumn + originId;
            }
            else if (typeof(IWidgetContainer).IsAssignableFrom(typeof(T)))
            {
                return Constants.ID_Prefix_WidgetContainer + originId;
            }
            else if (typeof(IWidget).IsAssignableFrom(typeof(T)))
            {
                return Constants.ID_Prefix_Widget + originId; 
            } 

            return originId;
        }

        /// <summary>
        /// Generate the Drag&Drop initialize JavaScript which using CTX.Widgets JS library.
        /// </summary>
        /// <param name="sortableColumnClass">the sortable column's class name</param>
        /// <param name="draggableWidgetContainerClass">the draggable widgets' class name</param>
        /// <param name="lockWidgetContainerClass">the element class name used to lock widget container</param>
        /// <param name="sortableHandleClass">the element class which used to handle the sortable event</param>
        /// <param name="sortablePlaceHolderClass">the class used to dsiplay at the position of the widget container which is dragging</param>
        /// <param name="templatePostBackTrigger">the class used to trigger the template's update refresh after drag a widget container from one column to another column successfully or add a new widget successfully.</param>
        /// <param name="newWidgetClass">the element class which used to indicate the new widget template element(which is draggable to the column to add a new widget)</param>
        /// <param name="newWidgetTitleClass">the new widget title class used to indicate the newly added widget's title before the template has been refreshed.</param>
        /// <returns></returns>
        public static string GenerateDragDropInitializeScript(string sortableColumnClass, string draggableWidgetContainerClass,
                                                              string lockWidgetContainerClass, string sortableHandleClass,
                                                              string sortablePlaceHolderClass, string templatePostBackTriggerClass,
                                                              string newWidgetClass, string newWidgetTitleClass = "")
        {
            StringBuilder scriptBuilder = new StringBuilder();
            scriptBuilder.Append("if(typeof(CTX.Widgets) !=\"undefined\"){");
            scriptBuilder.AppendFormat("jQuery(document).ready(function(){{CTX.Widgets.initialize('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');}});", //"CTX.Widgets.initialize('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');"
                            sortableColumnClass,
                            draggableWidgetContainerClass,
                            newWidgetClass,
                            newWidgetTitleClass,
                            sortableHandleClass,
                            lockWidgetContainerClass,
                            sortablePlaceHolderClass,
                            String.IsNullOrEmpty(templatePostBackTriggerClass) ? "" : "." + templatePostBackTriggerClass
                            );
            scriptBuilder.Append("}");

            return scriptBuilder.ToString();
        }
    }
} 